#ifndef _BSP_LCD_H_
#define _BSP_LCD_H_
/*
    board connects to 4 wire SPI LCD
    LCD driver IC: ST7789V2
*/

#define USE_HORIZONTAL 0 // 设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏

#if USE_HORIZONTAL == 0 || USE_HORIZONTAL == 1
#define LCD_W 240
#define LCD_H 320

#else
#define LCD_W 320
#define LCD_H 240
#endif

#define LCD_USE_DMA

#define LCD_SPI_GPIO_GROUP GPIOA
#define LCD_SPI_GPIO_RCU   RCU_GPIOA
#define LCD_SPI            SPI0
#define LCD_SPI_RCU        RCU_SPI0
#define LCD_SPI_CLK        GPIO_PIN_5
#define LCD_SPI_MISO       GPIO_PIN_6
#define LCD_SPI_MOSI       GPIO_PIN_7

#define LCD_CSS_GROUP      GPIOB
#define LCD_CSS_GPIO_RCU   RCU_GPIOB
#define LCD_CSS_PIN        GPIO_PIN_0 /* PB0 */

#define LCD_BLK_GROUP      GPIOB
#define LCD_BLK_GPIO_RCU   RCU_GPIOB
#define LCD_BLK_PIN        GPIO_PIN_1 /* PB1 */

#define LCD_RES_GROUP      GPIOC
#define LCD_RES_GPIO_RCU   RCU_GPIOC
#define LCD_RES_PIN        GPIO_PIN_4 /* PC4 */

#define LCD_DC_GROUP       GPIOC
#define LCD_DC_GPIO_RCU    RCU_GPIOC
#define LCD_DC_PIN         GPIO_PIN_5 /* PC5 */

/* ----------------------------------------------------------- */

#include <gd32f10x.h>

#define LCD_RES_Clr() gpio_bit_write(LCD_RES_GROUP, LCD_RES_PIN, RESET) // RES
#define LCD_RES_Set() gpio_bit_write(LCD_RES_GROUP, LCD_RES_PIN, SET)

#define LCD_DC_Clr()  gpio_bit_write(LCD_DC_GROUP, LCD_DC_PIN, RESET) // DC
#define LCD_DC_Set()  gpio_bit_write(LCD_DC_GROUP, LCD_DC_PIN, SET)

#define LCD_CS_Clr()  gpio_bit_write(LCD_CSS_GROUP, LCD_CSS_PIN, RESET) // CS
#define LCD_CS_Set()  gpio_bit_write(LCD_CSS_GROUP, LCD_CSS_PIN, SET)

#define LCD_BLK_Clr() gpio_bit_write(LCD_BLK_GROUP, LCD_BLK_PIN, RESET) // BLK
#define LCD_BLK_Set() gpio_bit_write(LCD_BLK_GROUP, LCD_BLK_PIN, SET)

void lcd_spi_write_byte(uint8_t dat);                                   // 模拟SPI时序
void lcd_spi_write_2byte(uint16_t dat);                                 // 写入两个字节
void lcd_write_reg(uint8_t dat);                                        // 写入一个指令
void lcd_set_range(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); // 设置坐标函数                                                      // LCD初始化

int lcd_init(void);

#ifdef LCD_USE_DMA
#define LCD_DMA            DMA0
#define LCD_DMA_RCU        RCU_DMA0
#define LCD_DMA_TX_CHANNEL DMA_CH2

#endif

#endif
